package com.xinlianshiye.im

import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper
import java.util.*

class Result private constructor() //禁止被new调用
    : HashMap<String?, Any>() {
    override fun put(key: String?, value: Any): Result {
        super.put(key, value)
        return this
    }

    override fun toString(): String {
        try {
            return mapper.writeValueAsString(this)
        } catch (e: JsonProcessingException) {
            e.printStackTrace()
        }
        return super.toString()
    }

    companion object {
        private val mapper = ObjectMapper()
        @JvmStatic
        fun of(): Result {
            return Result()
        }

        @JvmStatic
        fun of(code: Int): Result {
            val er = Result()
            er["code"] = code
            return er
        }

        @JvmStatic
        fun of(code: Int, msg: String): Result {
            val er = Result()
            er["code"] = code
            er["msg"] = msg
            return er
        }

        @JvmStatic
		fun of(code: Int, msg: String, data: Any): Result {
            val er = Result()
            er["code"] = code
            er["msg"] = msg
            er["data"] = data
            return er
        }

        @JvmStatic
		fun success(data: Any): Result {
            return of(200, "success", data)
        }
    }
}